package org.example.easybuy.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.easybuy.entity.CollectionBox;
import org.example.easybuy.service.CollectionBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class CollectionBoxServiceImpl implements CollectionBoxService {


    ObjectMapper objectMapper=new ObjectMapper();
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String intoCollectionBox(Map<String, String> map) throws JsonProcessingException {
        String loginName=map.get("loginName");
        CollectionBox c=objectMapper.readValue(map.get("collectionBox"), CollectionBox.class);
        List<CollectionBox> list;
        try{
            if(!stringRedisTemplate.hasKey(loginName+"_collect"))
            {
                //第一次添加
                list=new ArrayList<>();
                list.add(c);
            }else{
                //获取收藏夹集合
                list=objectMapper.readValue(stringRedisTemplate.opsForValue().get(loginName+"_collect"),
                        objectMapper.getTypeFactory().constructCollectionType(List.class,CollectionBox.class));
                //先判断收藏的商品是否已经存在
                boolean flag=false;
                for(CollectionBox coll:list)
                {
                    if(c.getId()==coll.getId())
                    {
                        flag=true;
                        break;
                    }
                }
                //没有重复就添加
                if(!flag)
                {
                    if(list.size()==6)
                    {
                        //移除第一个
                        list.remove(0);
                        list.add(c);
                    }else{
                        list.add(c);
                    }
                }else{
                    return "repeat";
                }
            }
            //将list存入redis
            stringRedisTemplate.opsForValue().set(loginName+"_collect",objectMapper.writeValueAsString(list));
        }catch(RuntimeException e)
        {
            return "error";
        }
        return "yes";
    }

    @Override
    public boolean delCollectById(int id, String loginName) throws JsonProcessingException {
        //获取收藏夹集合
        List<CollectionBox> list=objectMapper.readValue(stringRedisTemplate.opsForValue().get(loginName+"_collect"),
                objectMapper.getTypeFactory().constructCollectionType(List.class,CollectionBox.class));
        boolean flag=false;
        for(CollectionBox collect:list)
        {
            if(collect.getId()==id)
            {
                //从list中移除
                flag=list.remove(collect);
                break;
            }
        }
        //从list中移除了
        if(flag)
        {
            //重新覆盖list
            stringRedisTemplate.opsForValue().set(loginName+"_collect",objectMapper.writeValueAsString(list));
        }else{
            return false;
        }
        return true;
    }
}
